package com.scmt.hmzd.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.scmt.hmzd.entity.HmProject;
import com.scmt.hmzd.service.IHmProjectService;
import com.scmt.core.common.vo.PageVo;
import com.scmt.core.common.vo.SearchVo;
import com.scmt.hmzd.mapper.HmProjectMapper;
import com.scmt.core.utis.FileUtil;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.LinkedHashMap;
import java.util.List;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Map;

/**
 * @author
 **/
@Service
public class HmProjectServiceImpl extends ServiceImpl<HmProjectMapper, HmProject> implements IHmProjectService {
    @Autowired
    @SuppressWarnings("SpringJavaAutowiringInspection")
    private HmProjectMapper hmProjectMapper;

    @Override
    public IPage<HmProject> queryHmProjectListByPage(HmProject hmProject, SearchVo searchVo, PageVo pageVo) {
        int page = 1;
        int limit = 10;
        if (pageVo != null) {
            if (pageVo.getPageNumber() != 0) {
                page = pageVo.getPageNumber();
            }
            if (pageVo.getPageSize() != 0) {
                limit = pageVo.getPageSize();
            }
        }
        Page<HmProject> pageData = new Page<>(page, limit);
        QueryWrapper<HmProject> queryWrapper = new QueryWrapper<>();
        if (hmProject != null) {
            queryWrapper = LikeAllField(hmProject, searchVo);
        }
        IPage<HmProject> result = hmProjectMapper.queryHmProjectListByPage(queryWrapper, pageData);
        return result;
    }

    @Override
    public void download(HmProject hmProject, HttpServletResponse response) {
        List<Map<String, Object>> mapList = new ArrayList<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        QueryWrapper<HmProject> queryWrapper = new QueryWrapper<>();
        if (hmProject != null) {
            queryWrapper = LikeAllField(hmProject, null);
        }
        List<HmProject> list = hmProjectMapper.selectList(queryWrapper);
        for (HmProject re : list) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("名称", re.getTitle());
            map.put("图片地址", re.getCover());
            map.put("价格", re.getPrice());
            map.put("时长", re.getTimeLong());
            map.put("项目介绍", re.getIntroduce());
            map.put("注意事项", re.getExplainInfo());
            map.put("下单须知", re.getNotice());
            map.put("排名", re.getTop());
            map.put("有效状态", re.getDelFlag());
            map.put("原始价格", re.getInitPrice());
            map.put("项目简介", re.getSubTitle());
            mapList.add(map);
        }
        FileUtil.createExcel(mapList, "exel.xlsx", response);
    }

    @Override
    public List<HmProject> queryAllHmProjectList(HmProject hmProject) {
        QueryWrapper<HmProject> queryWrapper = new QueryWrapper<>();
        if (hmProject != null) {
            queryWrapper = LikeAllField(hmProject, null);
        }
        return hmProjectMapper.selectList(queryWrapper);
    }

    /**
     * 功能描述：构建模糊查询
     *
     * @param hmProject 需要模糊查询的信息
     * @return 返回查询
     */
    public QueryWrapper<HmProject>


    LikeAllField(HmProject hmProject, SearchVo searchVo) {
        QueryWrapper<HmProject> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(hmProject.getId())) {
            queryWrapper.and(i -> i.eq("hm_project.id", hmProject.getId()));
        }
        if (StringUtils.isNotBlank(hmProject.getTitle())) {
            queryWrapper.and(i -> i.like("hm_project.title", hmProject.getTitle()));
        }
        if (StringUtils.isNotBlank(hmProject.getCover())) {
            queryWrapper.and(i -> i.like("hm_project.cover", hmProject.getCover()));
        }
        if (hmProject.getTimeLong() != null) {
            queryWrapper.and(i -> i.eq("hm_project.time_long", hmProject.getTimeLong()));
        }
        if (hmProject.getMaxTime() != null) {
            queryWrapper.and(i -> i.like("hm_project.max_time", hmProject.getMaxTime()));
        }
        if (StringUtils.isNotBlank(hmProject.getIntroduce())) {
            queryWrapper.and(i -> i.like("hm_project.introduce", hmProject.getIntroduce()));
        }
        if (StringUtils.isNotBlank(hmProject.getExplainInfo())) {
            queryWrapper.and(i -> i.like("hm_project.explain_info", hmProject.getExplainInfo()));
        }
        if (StringUtils.isNotBlank(hmProject.getNotice())) {
            queryWrapper.and(i -> i.like("hm_project.notice", hmProject.getNotice()));
        }
        if (hmProject.getTop() != null) {
            queryWrapper.and(i -> i.like("hm_project.top", hmProject.getTop()));
        }
        if (hmProject.getDelFlag() != null) {
            queryWrapper.and(i -> i.like("hm_project.del_flag", hmProject.getDelFlag()));
        }
        if (hmProject.getCreateTime() != null) {
            queryWrapper.and(i -> i.like("hm_project.create_time", hmProject.getCreateTime()));
        }
        if (hmProject.getStar() != null) {
            queryWrapper.and(i -> i.like("hm_project.star", hmProject.getStar()));
        }

        /**
         * 如果传入了金额，就查比这个项目金额还大的项目
         */
        if (hmProject.getPrice() != null) {
            queryWrapper.and(i -> i.gt("hm_project.price", hmProject.getPrice()));
        }

        if (hmProject.getInitPrice() != null) {
            queryWrapper.and(i -> i.like("hm_project.init_price", hmProject.getInitPrice()));
        }
        if (StringUtils.isNotBlank(hmProject.getSubTitle())) {
            queryWrapper.and(i -> i.like("hm_project.sub_title", hmProject.getSubTitle()));
        }
        if (StringUtils.isNotBlank(hmProject.getTechnicianId())) {
            queryWrapper.inSql("hm_project.id", "SELECT project_id FROM hm_project_technician WHERE technician_id =" + "'" + hmProject.getTechnicianId() + "'");
        }
        if (searchVo != null) {
            if (searchVo.getStartDate() != null && searchVo.getEndDate() != null) {
                queryWrapper.lambda().and(i -> i.between(HmProject::getCreateTime, searchVo.getStartDate(), searchVo.getEndDate()));
            }
        }
        queryWrapper.lambda().and(i -> i.eq(HmProject::getDelFlag, 0));
        return queryWrapper;
    }
}
